using System;
using Habanero.Base;
using Habanero.Base.Exceptions;
using Habanero.BO.ClassDefinition;

namespace FireStarterModeller.BO
{
    public class DMInheritanceHierachyDiscriminatorsAreEqual : IBusinessObjectRule
    {
        private DMInheritanceRelationship _inheritanceRelationship;
//
//        public DMInheritanceHierachyDiscriminatorsAreEqual(DMInheritanceRelationship inheritanceRelationship)
//        {
//            if (inheritanceRelationship == null) throw new ArgumentNullException("inheritanceRelationship");
//            _inheritanceRelationship = inheritanceRelationship;
//        }

        public bool IsValid()
        {
            throw new HabaneroApplicationException("The IsValid is no longer Valid you should use the IsValid(bo) method");
        }

        public bool IsValid(IBusinessObject bo)
        {
            _inheritanceRelationship = (DMInheritanceRelationship) bo;
            if (this._inheritanceRelationship.SuperClass == null) return true;
            DMInheritanceRelationship grandParentRelationship = this._inheritanceRelationship.SuperClass.InheritanceSuperClassRelationship;
            if (grandParentRelationship == null) return true;
            if (grandParentRelationship.InheritanceType == ORMapping.ConcreteTableInheritance) return true;
            return this._inheritanceRelationship.Discriminator == grandParentRelationship.Discriminator;
        }

        public string Name
        {
            get { return "DMInheritanceHierachyDiscriminatorsAreEqual"; }
        }

        public string Message
        {
            get
            {
                return
                    string.Format(
                        "The discriminator for the superclass of '{0}' must be the same property as the superclass's superclass",
                        _inheritanceRelationship.SubClass);
            }
        }

        public ErrorLevel ErrorLevel
        {
            get { return ErrorLevel.Warning; }
        }
    }
}